#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "mydata/model.h"
#include "mydb.h"
#include "testhelp.h"

void tag_testing(void)
{
    char *tags[5] = {"tag1","tag2","tag3","tag4","tag5"};
    uint64_t note_id = 10068;
    Tag new_tag = {
	.note_id = note_id,
	.owner_id = 1,
    };
    // 1.insert tags.
    int i;
    for (i = 0; i < 5; i++) {
	if (!mydata_tag_exist(note_id, tags[i])) {
	    new_tag.name = tags[i];
	    mydata_insert_tag(&new_tag);
	} }
    // 2.get tags.
    ArrayTable  notetags;
    mydata_get_tags(note_id, &notetags);
    for (i = 0; i < notetags.size; i++) {
	Tag tmp = ((Tag *)notetags.ptr)[i];
	printf("%s\t%s\t%d\n", tmp.name,tmp.time_create,tmp.count_ref);
	//mydata_remove_tag(10068, tmp.name);
    }
    // 3.remove tags.
    mydata_remove_tags(note_id);
}

void user_testing(void)
{
    // 1. insert a user.
    char *nickname = "netbook";
    char *mail = "testing@netbook.com";
    char *passwd = "testing123";
    User new_user = {
	.nickname = strdup(nickname),
	.mail = strdup(mail),
	.passwd = strdup(passwd),
	.attribute = 0,
    };
    // 1.1 check if the mail is already registered.
    uint32_t uid;
    if (!mydata_user_mail_exist(mail, &uid)) {
	mydata_insert_user(&new_user); 
	user_dump(&new_user);
	uid = new_user.id;
	puts("press enter to update: ");
	getchar();
	// update user.
	free(new_user.nickname);
	new_user.nickname = strdup("testing");
	new_user.attribute = 1;
	mydata_update_user(&new_user, USER_NICKNAME|USER_ATTRIBUTE);
    }

    // 2. get the user.
    // 2.1 by id. 
    User *get_user1;
    if (!mydata_get_user_by_id(uid, &get_user1)) {
	user_dump(get_user1); }
    // 2.2 by mail
    User *get_user2;
    if (!mydata_get_user_by_mail(mail, &get_user2)) {
	user_dump(get_user2); }
    // remove user.
    mydata_remove_user(uid);
}

void note_testing(void)
{
    uint32_t user_id = 1;
    Note note = {
	.owner_id = user_id,
	.title = strdup("testing title..."),
	.content = strdup("testing content..."),
	.version = 0,
	.attribute = 0,
    };
    // insert
    mydata_insert_note(&note);
    note_dump(&note);
    if (mydata_own_note(user_id, note.id)) {
	// update
	free(note.title);
	note.title = strdup("XXXXXXXXXXXXXXXXXX");
	free(note.content);
	note.content = strdup("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
	puts("press enter to update: ");
	getchar();
	mydata_update_note(&note, NOTE_TITLE|NOTE_CONTENT);
	Note *note_get;
	mydata_get_note_by_id(note.id, &note_get);
	note_dump(note_get);
	puts("press enter to delete: ");
	getchar();
	mydata_remove_note(note.id);
    }
}

void session_testing(void)
{
    // 3. insert a session.
    // 3.1 check if the mail&passwd match.
    char *mail = "testing@netbook.com";
    char *passwd = "testing123";
    uint32_t user_id;
    char *uuid;
    if (mydata_mail_passwd_match(mail, passwd, &user_id)) {
	if (!mydata_session_user_exist(user_id, &uuid)) {
	    Session new_session = {
		.user_id = user_id,
		.attribute = 0,
	    };
	    if (mydata_insert_session(&new_session) < 0) {
		return ; }
	    // login now
	    session_dump(&new_session);
	    uuid = strdup(new_session.uuid);
	    // get user.
	    User *user_get;
	    mydata_get_user_by_mail(mail, &user_get);
	    user_dump(user_get);

	    if (mydata_session_exist(uuid, &user_id)) {
	    // logout
	    if (mydata_session_exist(uuid, &user_id)) {
		mydata_remove_session(uuid); }
	}
    }}
    return ;
}

int main(void)
{
    // init database API to mydata_real.
    if (mydata_init("mydata_real") < 0) {
	exit(0); }
    // start testing.
    //tag_testing();
    uint64_t num;
    mydata_get_all_public_note_number(&num);
    printf("%lld\n", num);
    ArrayTable notelist;
    mydata_get_all_public_note_list(0, num, &notelist);
    uint32_t i;
    for (i = 0;i < notelist.size; i++) {
	note_dump(((Note *)(notelist.ptr))+i);
    }
    return 0;
}

